package com.app.mingluxing.my;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.AbDateUtil;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2015/9/9.
 */
public class BabyModifyBirthdayActivity extends BaseActivity{
    private static final int MODIFY_MY_BIRTHDAY_CODE = 10001;
    private TextView tv_birthday;
    private String ID;
    private String SEX;
    private String NAME;
    private String PHOTO;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_birthday);
        init();
        ID = getIntent().getStringExtra("ID");
        SEX = getIntent().getStringExtra("sex");
        NAME = getIntent().getStringExtra("name");
        PHOTO = getIntent().getStringExtra("photo");
    }

    private void init() {
        ImageView tv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        tv_title.setText(getResources().getString(R.string.modify_birthday));
        tv_right.setText(getResources().getString(R.string.done));
        tv_birthday= (TextView) findViewById(R.id.tv_birthday);
        tv_birthday.setText(sp.getString(Constant.BABYBIRTHDAY, ""));
        tv_birthday.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_right:
                if(CommonUtils.isNull(tv_birthday.getText().toString().trim())){
                    showToast("生日不能为空");
                    return;
                }

                upDateUserInfo();
                break;

            case R.id.tv_birthday:

                //选择生日
                String birthday = sp.getString(Constant.BABYBIRTHDAY, "");
                Date date = null;

                if (!CommonUtils.isNull(birthday)) {
                    date = AbDateUtil.getDateByFormat(birthday, AbDateUtil.dateFormatYMD);
                }

                Calendar c = Calendar.getInstance();
                if (date != null) {
                    c.setTime(date);
                }

                new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                          int dayOfMonth) {

                        String month = (monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1) + "";
                        String day = dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + "";

                        tv_birthday.setText(year + "-" + month + "-" + day);

                    }

                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                        c.get(Calendar.DAY_OF_MONTH)).show();

                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

    /**
     * 提交用户修改的信息
     */
    private void upDateUserInfo() {

        JSONObject j = new JSONObject();
        j.put("id", ID);
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("birthDay", tv_birthday.getText().toString().trim());
        j.put("name", NAME);
        j.put("sex", SEX);
        j.put("photo", PHOTO);

        HttpUtil.setPostRequest("更新中...", this, Constant.WEB_ROOT + Constant.UPDATECHILDINFO,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {
                                SharedPreferences.Editor edit = sp.edit();
                                edit.putString(Constant.BABYBIRTHDAY, "");
                                edit.apply();
                                //数据传递
                                String settingsbirthday = tv_birthday.getText().toString().trim();
                                Intent data = new Intent();
                                data.putExtra("modifyBirthday", settingsbirthday);
                                setResult(MODIFY_MY_BIRTHDAY_CODE, data);
                                finish();
                            } else {

                                showToast(Constant.showErrorMsg(result.getInt("code"), BabyModifyBirthdayActivity.this));

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);

                        }
                    }
                }, null);
    }
}
